﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Collections.Generic;

namespace Coding4Fun.BoxingBots.UserConsole
{
    public enum RobotRings
    {
        Reverse,
        PunchRight,
        StrafeRight,
        TurnRight,
        Forward,
        TurnLeft,
        StrafeLeft,
        PunchLeft
    }

    public partial class ringRobot : UserControl
    {

        double _totalDamage = 0.0;
        double _totalDamageTarget = 0.0;

        public ringRobot()
        {
            InitializeComponent();
            EventController.MoveRobot += MoveRobot;
            this.Loaded += new RoutedEventHandler(ringRobot_Loaded);
        }

        void ringRobot_Loaded(object sender, RoutedEventArgs e)
        {
            outerRingImages.Add(ImgForward);
            outerRingImages.Add(ImgReverse);
            outerRingImages.Add(ImgStrafeLeft);
            outerRingImages.Add(ImgStrafeRight);
            outerRingImages.Add(ImgTurnLeft);
            outerRingImages.Add(ImgTurnRight);
        }

        List<Image> outerRingImages = new List<Image>();

        private void MoveRobot(object sender, EventArgs e)
        {
            switch ((RobotRings)sender)
            {
                case RobotRings.Forward:
                    ImgForward.Opacity = 1.0;
                    break;
                case RobotRings.Reverse:
                    ImgReverse.Opacity = 1.0;
                    break;
                case RobotRings.StrafeLeft:
                    ImgStrafeLeft.Opacity = 1.0;
                    break;
                case RobotRings.StrafeRight:
                    ImgStrafeRight.Opacity = 1.0;
                    break;
                case RobotRings.TurnLeft:
                    ImgTurnLeft.Opacity = 1.0;
                    break;
                case RobotRings.TurnRight:
                    ImgTurnRight.Opacity = 1.0;
                    break;
                case RobotRings.PunchLeft:
                    ImgPunchRight.Opacity = 1.0;
                    break;
                case RobotRings.PunchRight:
                    ImgPunchLeft.Opacity = 1.0;
                    break;
            }
        }

        public void UpdateUX()
        {
            foreach (Image im in outerRingImages)
            {
                if (im.Opacity > 0.0) im.Opacity -= .02;
            }

            if (ImgPunchLeft.Opacity > 0.0) ImgPunchLeft.Opacity -= .005;
            if (ImgPunchRight.Opacity > 0.0) ImgPunchRight.Opacity -= .005;


            // THIS ASSUMES THAT SETDAMAGE() HAS BEEN CALLED PASSING IN A DAMAGE VALUE FROM 0 TO 8.
            // THE FOLLOWING SIMPLY IS A "DUMB ANIMATION" WITH _totalDamage BEING SET FROM SETDAMAGE()

            // ANIMATE THE DAMAGE RING
            _totalDamage += (_totalDamageTarget - _totalDamage) * .2;
            drawTimeBlade(figure_RingDamage, _totalDamage, 232.0, 45.0);
        }

        #region TEMPORARY CODE TO TEST DAMAGE RING EVENTS 

        int TEMPCOUNT = 0;

        double TEMPDAMAGE = 0.0;

        private void TEMP_TestDamage()
        {
            TEMPCOUNT++;
            if ( TEMPCOUNT > 200 ) 
            {
                TEMPCOUNT = 0;
                TEMPDAMAGE++;
                if ( TEMPDAMAGE > 8.0 ) TEMPDAMAGE = 0.0;
                SetDamage(TEMPDAMAGE, null );
            }
        }

        #endregion

        private void SetDamage(object sender, EventArgs e)
        {
            // THERE ARE 8 DAMAGE BLADES, SO EACH DAMAGE INCREASE NEEDS TO BE ANIMATED IN STEPS OF 45 DEGREES ( or (Math.Pi*2)/8.0 radians (approx. 0.7854 ))
            // SO IN THIS CASE, (sender) SHOULD BE A DOUBLE FROM 0.0 - 8.0 THAT INDICATES THE TOTAL DAMAGE AS DEFINED BY THE GAME SYSTEM.
            double raw = Convert.ToDouble(sender);
            if (raw >= 0.0 && raw <= 8.0)
            {
                _totalDamageTarget = raw/8.0;
            }
        }

        private void drawTimeBlade(PathFigure blade, double angleNormal, double radius, double bladeWidth)
        {
            double angle = angleNormal * 6.28;
            Size innerRadius = new Size(radius, radius);
            Size outerRadius = new Size(radius + bladeWidth, radius + bladeWidth);
            Point innerArcStart = new Point(radius, 0.0);
            Point outerArcStart = new Point(radius + bladeWidth, 0.0);

            blade.Segments.Clear();
            blade.StartPoint = innerArcStart;
            blade.Segments.Add(new LineSegment(outerArcStart, true));

            bool isLarge = false;
            if (Math.Abs(angle) > 3.142) isLarge = true;

            blade.Segments.Add(new ArcSegment(
                new Point(Math.Cos(angle) * outerRadius.Height, Math.Sin(angle) * outerRadius.Height),
                outerRadius, 0, isLarge, SweepDirection.Clockwise, true));

            blade.Segments.Add(new LineSegment(
                new Point(Math.Cos(angle) * innerRadius.Width, Math.Sin(angle) * innerRadius.Width),
                true));

            blade.Segments.Add(new ArcSegment(innerArcStart, innerRadius, 0, isLarge, SweepDirection.Counterclockwise, true));

        }


    }
}
